package top.someapp.dxf2dsvg.dxf;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zwz
 * Created on 2020-04-17
 * @see <a href="http://help.autodesk.com/view/OARX/2018/CHS/?guid=GUID-A9FD9590-C97B-4E41-9F26-BD82C34A4F9F">
 * 关于 DXF TABLES 段 (DXF)</a>
 */
public class SectionTables implements Section {
    private List<Block> blocks = new ArrayList<>(); // we will handle it on later version
    private List<Layer> layers = new ArrayList<>();
    private final List<Item> itemList = Collections.emptyList();
    private boolean layerMetaStart;
    private boolean layerMetaEnd;
    private boolean layerStart;
    private boolean layerEnd;

    @Override
    public SectionName name() {
        return SectionName.TABLES;
    }

    @Override
    public List<Item> items() {
        return itemList;
    }

    @Override
    public void onItemFound(@Nonnull Item item) {
//        System.out.println("item in TABLES: " + item);
        if ("LAYER".equals(item.value)) { // we only care for layer
            if (item.code == 2) { // layer metadata start
                layerMetaStart = true;
            } else if (item.code == 0) { // layer start
                layerMetaEnd = true;
                layerStart = true;
            }
            return;
        }
        if (layerMetaStart && !layerMetaEnd) { // in layer metadata
            if (70 == item.code) {
                layerMetaEnd = true;
                layerMetaStart = false;
                return;
            }
        }
        if (layerStart && !layerEnd) { // in layer data
            if (2 == item.code && item.isCodeStartWithPadding()) { // layer name
                System.out.println("found layer: " + item.value);
                layers.add(new Layer(item.value));
                layerStart = false;
            }
            layerEnd = "ENDTAB".equals(item.value);
        }
    }

    List<Layer> getLayers() {
        return layers;
    }

    @Override
    public String toString() {
        return "SectionTables{" +
                "layers=" + layers +
                '}';
    }
}
